package com.lpf.json.fastjson.SerializeFilter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.PascalNameFilter;

public class TestNameFilter {

	public static void main(String[] args) {
		User user = new User();
		user.setId(9L);
		user.setName("挖坑埋你");

		String jsonString = JSON.toJSONString(user); // 序列化的时候传入filter
		System.out.println("普通序列化：" + jsonString + "\n");

		NameFilter filter = new NameFilter() {

			@Override
			public String process(Object object, String name, Object value) {
				System.out.println("----------------object=" + object);
				System.out.println("----------------name=" + name);
				System.out.println("----------------value=" + value);
				System.out.println("");

				if ("id".equals(name)) {
					return name + "$";
				}
				return name;
			}
		};

		jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
		System.out.println("NameFilter序列化：" + jsonString + "\n");

		// fastjson内置一个PascalNameFilter，用于输出将首字符大写的Pascal风格
		jsonString = JSON.toJSONString(user, new PascalNameFilter()); // 序列化的时候传入filter
		System.out.println("PascalNameFilter序列化：" + jsonString + "\n");
	}

}
